package com.paotui.controller;

import cn.hutool.json.JSONUtil;
import com.paotui.common.R;
import com.paotui.pojo.Users;
import com.paotui.service.impl.UsersServiceImpl;
import org.springframework.web.bind.annotation.CrossOrigin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/users/query")
public class UsersQueryController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String phone = request.getParameter("UsersTelephone");
        String password = request.getParameter("UsersPassword");
        UsersServiceImpl impl = new UsersServiceImpl();
        List<Users> list = impl.list();
        impl.getSqlSession().close();
        Integer id = -1;
        for (Users user : list) {
            if (user.getUsersTelephone().equals(phone) && user.getUsersPassword().equals(password)) {
                id = user.getUsersID();
                break;
            }
        }
        R r;
        if (id != -1) {
            r = R.success("登陆成功", id);
        } else {
            r = R.fail("登陆失败，未查询到该用户");
        }
        response.getWriter().write(JSONUtil.toJsonStr(r));
    }
}
